package cn.Alibaba.pet.service.impl;

import cn.Alibaba.basic.util.BaiduAuditUtils;
import cn.Alibaba.basic.util.LoginContext;
import cn.Alibaba.org.domain.Employee;
import cn.Alibaba.org.mapper.EmployeeMapper;
import cn.Alibaba.pet.domain.Pet;
import cn.Alibaba.pet.domain.PetDetail;
import cn.Alibaba.pet.domain.PetOnlineAuditLog;
import cn.Alibaba.pet.mapper.PetDetailMapper;
import cn.Alibaba.pet.mapper.PetMapper;
import cn.Alibaba.pet.service.IPetService;
import cn.Alibaba.basic.service.impl.BaseServiceImpl;
import cn.Alibaba.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Transactional
    @Override
    public void save(Pet pet) {
        super.save(pet);

        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delPetDetailByPetId(id);
    }

    // 上架
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);

        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        for (Long id : ids) {
            Pet pet = petMapper.loadOne(id);

            Boolean txBoo = BaiduAuditUtils.TextCensor(pet.getName());
           // Boolean imgBoo = BaiduAuditUtils.ImgCensor(pet.getResources());

            if (txBoo ){
                pet.setOnsaletime(new Date());
                pet.setState(1);
                petMapper.update(pet);

                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(1);
                log.setAuditTime(new Date());
                log.setNote("上架成功");
                log.setAuditId(employee.getId());

            }else {

                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(-1);
                log.setAuditTime(new Date());
                log.setNote("上架失败");
                log.setAuditId(employee.getId());
            }


        }




    }


    // 下架
    @Override
    public void offsale(List<Long> ids) {

        Date offsaletime = new Date();

        petMapper.offsale(offsaletime,ids);


    }
}
